// Ionic_SDK
// 
// 保佑没Bug 
//
// Created by AS on 2019/5/21. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "ReplyItemCell.h"
#import "EdgeUILabel.h"
#import "UserInfo.h"
@interface ReplyItemCell ()
/**  */
@property (nonatomic,strong) UIImageView *avatarImage;
/**  */
@property (nonatomic,strong) UILabel *nameLabel;
/**  */
@property (nonatomic,strong) UILabel *timeLabel;
/**  */
@property (nonatomic,strong) UILabel *contentLabel;
/**  */
@property (nonatomic,strong) UIButton *commentBtn;
/**  */
@property (nonatomic,strong) UIImageView *commentIcon;
/**  */
@property (nonatomic,strong) UILabel *commentLabel;

/**  */
@property (nonatomic,strong) UIButton *praiseBtn;
/**  */
@property (nonatomic,strong) UIImageView *praiseIcon;
/**  */
@property (nonatomic,strong) UILabel *praiseLabel;

@end

@implementation ReplyItemCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubview];
    }
    return self;
}

- (void)initSubview {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.avatarImage = [UIImageView new];
    [self.avatarImage setLayerWithRadius:20 color:ClearColor width:0];
    [self.contentView addSubview:self.avatarImage];
    
    self.nameLabel = [UILabel new];
    self.nameLabel.textColor = HexRGB(0xff8110);
    self.nameLabel.font = Font14;
    [self.contentView addSubview:self.nameLabel];
    
    self.timeLabel = [UILabel new];
    self.timeLabel.textColor = HexRGB(0x888888);
    self.timeLabel.font = Font12;
    [self.contentView addSubview:self.timeLabel];
    
    self.contentLabel = [UILabel new];
    self.contentLabel.font = Font16;
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.textColor = HexRGB(0x222222);
    [self.contentView addSubview:self.contentLabel];
    
    self.commentLabel = [UILabel new];
    self.commentLabel.font = Font14;
    self.commentLabel.text = @"回复";
    self.commentLabel.textColor = HexRGB(0x888888);
    [self.contentView addSubview:self.commentLabel];
    
    self.commentIcon = [UIImageView new];
    self.commentIcon.image = [UIImage imageNamed:@"reply"];
    [self.contentView addSubview:self.commentIcon];
    
    self.commentBtn = [UIButton new];
    [self.commentBtn addTarget:self action:@selector(replyAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.commentBtn];
    
    self.praiseLabel = [UILabel new];
    self.praiseLabel.font = Font14;
    self.praiseLabel.textColor = HexRGB(0x888888);
    [self.contentView addSubview:self.praiseLabel];
    
    self.praiseIcon = [UIImageView new];
    [self.contentView addSubview:self.praiseIcon];
    
    self.praiseBtn = [UIButton new];
    [self.praiseBtn addTarget:self action:@selector(praiseAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.praiseBtn];
    
    

    [self.praiseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-12);
        make.bottom.equalTo(self.contentView).offset(-15);
    }];
    
    [self.praiseIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.praiseLabel.mas_left).offset(-5);
        make.centerY.equalTo(self.praiseLabel);
        make.width.height.mas_offset(15);
    }];
    
    [self.praiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.praiseIcon).offset(-5);
        make.bottom.equalTo(self.praiseIcon).offset(5);
        make.right.equalTo(self.praiseLabel).offset(5);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.praiseBtn.mas_left).offset(-25);
        make.centerY.equalTo(self.praiseLabel);
    }];
    
    [self.commentIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.commentLabel.mas_left).offset(-5);
        make.centerY.equalTo(self.praiseIcon).offset(2);
        make.width.height.mas_offset(15);
    }];
    
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.commentIcon).offset(-5);
        make.bottom.equalTo(self.commentIcon).offset(5);
        make.right.equalTo(self.commentLabel).offset(5);
    }];
 
}

-(void)setModelF:(ReplyModelF *)modelF{
    _modelF = modelF;
    
    self.avatarImage.frame = modelF.avatarF;
    self.nameLabel.frame = modelF.nameF;
    self.contentLabel.frame = modelF.contentF;
    
    ReplyModel *model = modelF.model;
    CGSize size = [model.createTime sizeWithFont:Font14];
    
    self.timeLabel.frame = CGRectMake(self.nameLabel.x,self.nameLabel.maxY + 3, size.width + 50, size.height);
    [self.avatarImage sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage createImageWithColor:HexRGB(0xeeeeee)]];
    self.praiseLabel.text = [NSString stringWithFormat:@"%ld",model.likeNumber];
    self.nameLabel.text = model.nickName;
    self.timeLabel.text = model.createTime;
    if (model.replyModel) {
        self.contentLabel.attributedText = model.attributedText;
    }else{
        self.contentLabel.text = model.content;
    }
    if (model.isDz) {
        self.praiseIcon.image = [UIImage imageNamed:@"priseme"];
    }else{
        self.praiseIcon.image = [UIImage imageNamed:@"prise"];
    }
    
}


-(void)replyAction{
    if (self.replyModelBlock) {
        self.replyModelBlock(self.modelF.model);
    }
}

-(void)praiseAction{
    
    UserInfo *user = [UserInfo getUserInfo];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"loginToken"] = user.loginToken;
    dict[@"id"] = @(self.modelF.model.Id);
    if (!self.modelF.model.isDz) {
        
        NSString *url = @"/api/communicationCircleDetail/updateLike";
        [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
            if (!error) {
                if (!msg) {
                    self.modelF.model.likeNumber += 1;
                    self.modelF.model.isDz = YES;
                    self.praiseLabel.text = [NSString stringWithFormat:@"%ld",self.modelF.model.likeNumber];
                    self.praiseIcon.image = [UIImage imageNamed:@"priseme"];
                }
            }
        }];
    }else{
        
        NSString *url = @"/api/communicationCircleDetail/qxDz";
        [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
            if (!error) {
                if (!msg) {
                    self.praiseIcon.image = [UIImage imageNamed:@"prise"];
                    self.modelF.model.likeNumber -= 1;
                    self.modelF.model.isDz = NO;
                    self.praiseLabel.text = [NSString stringWithFormat:@"%ld",self.modelF.model.likeNumber];
                }
            }
        }];
    }
    
}


@end
